﻿@page
@model DotNetStar.TheKing.Web.Pages.Content.SearchModel
@{
    ViewBag.Title = "资料搜索";
}

@{
    var keyword = ViewContext.HttpContext.Request.Query["keyword"].ToString();
}
@using System.Text.RegularExpressions

<div class="col-12 text-center">
    <h2 class="mt-5">@ViewBag.Title</h2>
</div>

<div class="col-10 mx-auto mt-5 mb-5">
    <form asp-page="/Content/Search" method="get">
        <div class="input-group input-group-lg">
            <input type="text" name="keyword" value="@keyword" class="form-control" placeholder="输入你想查找的关键字" />
            <div class="input-group-append">
                <button type="submit" class="btn btn-dark"><i class="fa fa-search fa-2x"></i></button>
            </div>
        </div>
        <div class="mt-3">
            <ul class="list-inline">
                <li class="list-inline-item">热门标签：</li>
                @foreach (var item in Model.Tags)
                {
                    <li class="list-inline-item"><a class="text-primary" asp-route-keyword="@item.Name">@item.Name</a></li>
                }
            </ul>
        </div>
    </form>
</div>
@if (!keyword.IsNullOrWhiteSpace())
{
    <h4 class="mb-4">以下是您输入的关键字 “<span class="text-danger">@Model.SearchedKeyword?.Trim()</span>” 的搜索结果</h4>

    <ul class="list-group list-group-flush">
        @if (!Model.SearchResult.Any())
        {
            <li class="list-group-item text-center">
                对不起，搜索不到相关数据！
            </li>
        }
        @foreach (var item in Model.SearchResult)
        {
            <li class="list-group-item">
                <h4>
                    @if (!item.IsAsk)
                    {
                        @if (item.IsVideo)
                        {
                            <span title="多媒体视频" class="badge badge-info">视</span>
                        }
                        @if (item.IsOriginal)
                        {
                            <span title="原创内容" class="badge badge-success">原</span>
                        }

                        @if (!item.IsFree())
                        {
                            <span title="需要支付" class="badge badge-danger">售</span>
                        }
                    }
                    else
                    {
                        <span title="问题" class="badge badge-warning">问</span>
                        @if (item.IsLockComment)
                        {
                            <span title="已有最佳答案" class="badge badge-secondary"><i class="fa fa-lock"></i></span>
                        }
                    }
                    <a asp-page="/Content/Detail" asp-route-id="@item.Id" target="_blank">
                        @if (!keyword.IsNullOrWhiteSpace())
                        {
                            keyword.Split(' ').ForEach(value =>
                            {
                                if (!value.IsNullOrWhiteSpace())
                                {
                                    var matches = Regex.Matches(item.Subject, value, RegexOptions.IgnoreCase);
                                    foreach (Match match in matches)
                                    {
                                        item.Subject = item.Subject.Replace(match.Value, $"<span class='text-danger'>{match.Value}</span>");
                                    }
                                }
                            });

                        }
                        @Html.Raw(item.Subject)

                        @if (item.IsAsk)
                        {
                            <span class="text-sm" title="悬赏@(item.Cost)个金币">
                                <span class="fa fa-database text-golden"></span> @item.Cost
                            </span>
                        }
                    </a>
                </h4>
                <ul class="list-inline mt-3">
                    @if (item.Category != null)
                    {
                        <li class="list-inline-item">
                            <a asp-page="/Content/Index" asp-route-categoryId="@item.Category.Id">
                                @item.Category.Name
                            </a>
                        </li>
                    }
                    <li class="list-inline-item text-sm text-muted" title="作者"><a target="_blank" asp-page="/User/Index" asp-route-userId="@item.Author.Id"><img class="circle img-avatar-16" src="@item.Author.AvatarUrl" /> @item.Author.UserName</a></li>
                    <li class="list-inline-item text-sm text-muted d-sm-block d-md-none" title="阅读量"><i class="fa fa-eye"></i> @item.VisitNum</li>
                    <li class="list-inline-item text-sm text-muted" title="评论数"><i class="far fa-comments"></i> @item.CommentCount</li>
                    @if (!item.IsAsk)
                    {
                    <li class="list-inline-item text-sm text-muted" title="点攒数"><i class="far fa-thumbs-up"></i> @item.LikeNum</li>
                    }
                    <li class="list-inline-item text-sm text-muted" title="日期"><i class="far fa-calendar-alt"></i> @item.PostedTime.ToHumanDateTimeString()</li>

                </ul>
                <div class="mt-3">
                    @foreach (var tag in item.ContentTags)
                    {
                        <span class="badge badge-@if (tag.Name == keyword)
                        { <text>danger</text>}
                        else
                        { <text>light</text>} mrif">
                            @tag.Name
                        </span>
                    }
                </div>
            </li>
        }
    </ul>

    <div class="mt-3">
        @Html.Pagination(Model.SearchResult)
    </div>
}
else
{
    @section scripts{
        <script type="text/javascript">
            $('footer').addClass('fixed-bottom');
        </script>
    }
}